#!/bin/sh /etc/rc.common
#
# Copyright (C) 2016-2021 eGloo, Incorporated
#
# This is free software, licensed under the GNU General Public License v2.

START=50
STOP=50

USE_PROCD=1
PROG=/usr/sbin/@PACKAGE_TARNAME@

start_@PACKAGE_TARNAME@() {
    local autoconfig enabled external_if instance interfaces internal_if options

    instance="$1"

    source /usr/share/@PACKAGE_TARNAME@/functions.sh
    load_modules

    config_get_bool enabled "$instance" enabled 0
    [ "$enabled" -eq 0 ] && return 0

    [ ! -d /var/run/@PACKAGE_TARNAME@ ] && mkdir -p /var/run/@PACKAGE_TARNAME@

    config_get_bool autoconfig "$instance" autoconfig 1

    if [ "$autoconfig" -gt 0 ] ; then
        NETIFYD_AUTODETECT=yes
        interfaces="$(auto_detect_options)"
    else
        config_get internal_if "$instance" internal_if ""
        for ifn in $internal_if; do
            interfaces="${interfaces} -I ${ifn}"
        done
        config_get external_if "$instance" external_if ""
        for ifn in $external_if; do
            interfaces="${interfaces} -E ${ifn}"
        done
    fi

    config_get_bool options "$instance" options ""

    procd_open_instance
    procd_set_param command $PROG -R $options $interfaces
    procd_set_param file /etc/@PACKAGE_TARNAME@.conf
    procd_set_param respawn
    procd_close_instance
}

start_service() {
    config_load @PACKAGE_TARNAME@
    config_foreach start_@PACKAGE_TARNAME@ @PACKAGE_TARNAME@
}
